#!/bin/bash
#
# Resolve the location of the Philatelist installation.
# This includes resolving any symlinks.
PRG=$0
while [ -h "$PRG" ]; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '^.*-> \(.*\)$' 2>/dev/null`
    if expr "$link" : '^/' 2> /dev/null >/dev/null; then
        PRG="$link"
    else
        PRG="`dirname "$PRG"`/$link"
    fi
done

PHILATELIST_BIN=`dirname "$PRG"`

# absolutize dir
oldpwd=`pwd`
cd "${PHILATELIST_BIN}"
PHILATELIST_BIN=`pwd`
cd "${oldpwd}"

APP_NAME=Philatelist
TMP_DIR=`mktemp --directory`
DESKTOP_FILE=$TMP_DIR/Philatelist.desktop
cat << EOF > $DESKTOP_FILE
[Desktop Entry]
Version=1.0
Encoding=UTF-8
Name=Philatelist
Name[ru_RU]=Филателист
GenericName=Philatelist
GenericName[ru_RU]=Филателист
Comment=Stamp collecting software
Comment[ru_RU]=Коллекция почтовых марок
Keywords=philatelist;stamps;collection
Type=Application
Categories=Utility
Terminal=false
StartupNotify=true
Exec="$PHILATELIST_BIN/$APP_NAME.py" %u
Icon=$APP_NAME.png
EOF

# seems necessary to refresh immediately:
chmod 644 $DESKTOP_FILE

xdg-desktop-menu install $DESKTOP_FILE
xdg-icon-resource install --size  32 "$PHILATELIST_BIN/$APP_NAME-32.png"  $APP_NAME
xdg-icon-resource install --size  48 "$PHILATELIST_BIN/$APP_NAME-48.png"  $APP_NAME
xdg-icon-resource install --size  64 "$PHILATELIST_BIN/$APP_NAME-64.png"  $APP_NAME
xdg-icon-resource install --size 128 "$PHILATELIST_BIN/$APP_NAME-128.png" $APP_NAME

rm $DESKTOP_FILE
rm -R $TMP_DIR
